home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++Source Code Fmtr Folder / Src / Vector.h < prev   
Encoding:
C/C++ Source or Header  |  1992-04-27  |  2.4 KB  |  122 lines  |  [TEXT/MPS ]

  1. #ifndef __VECTOR__
  2. #define __VECTOR__    1
  3.  
  4. #pragma once
  5.  
  6. #ifndef __DATAAREA__
  7. #include "DataArea.h"
  8. #endif
  9.  
  10. #ifndef __TYPES__
  11. #include <types.h>
  12. #endif
  13.  
  14.  
  15. /*µ class Vector
  16. **    A vector is a collection of pointers to items.  The vector is zero-based.
  17. */
  18. class Vector : private DataArea {
  19. public:
  20.     Vector();
  21.  
  22.     int IVector();
  23.     int IVector(size_t initialSize, size_t increment);
  24.     int IVector(const Vector *aVector);
  25.     // Initialize the vector
  26.  
  27.  
  28.     void SetIncrement(size_t anIncrement);
  29.     int Length() const;
  30.     void *At(int i) const;
  31.  
  32.     Boolean AddItem(const void *anItem);
  33.     Boolean AddOnce(const void *anItem);
  34.     Boolean AddOnce(const Vector *aVector);
  35.     // Add the item to the Vector.  Return true if the item is now in the
  36.     // Vector, false if it isn't.  The AddOnce method adds the item to the
  37.     // Vector only if it isn't already in the Vector.  Comparison is done by
  38.     // pointer equality.  The AddOnce method given a reference to a Vector
  39.     // adds unique items from that Vector to this Vector.
  40.  
  41.     void AtPut(int i, const void *anItem);
  42.     // Set the i'th item to be anItem.
  43.  
  44.     int IndexOf(const void *anItem);
  45.     // Return the index of the item.  If the item is not in the
  46.     // Vector, return a negative value.
  47.  
  48.  
  49.     void Sort(int (*aCompareFunc)(const void *, const void *));
  50.     // Sort the vector using the comparison function passed in.
  51.  
  52.     virtual void MakeEmpty();
  53.     // Empty the Vector.
  54.  
  55.  
  56. protected:
  57.     void *_At(int i) const;
  58.     void _AtPut(int i, const void *anItem);
  59.     // Unsafe versions, allowing derived classes to sidestep
  60.     // bounds checks when they have proved the indices are valid
  61.  
  62. private:
  63.     int fLength;
  64. };
  65.  
  66.  
  67. //µ   Vector::IVector
  68. #pragma segment Vector
  69. inline int Vector::IVector()
  70. {
  71.     return (IVector(0, 4));
  72. }
  73.  
  74.  
  75. //µ   Vector::SetIncrement
  76. #pragma segment Vector
  77. inline void Vector::SetIncrement(size_t anIncrement)
  78. {
  79.     // Set the number of items that the vector is expanded by whenever
  80.     // it must be expanded.
  81.     DataArea::SetIncrement(anIncrement * sizeof(void *));
  82. }
  83.  
  84.  
  85. //µ   Vector::Length
  86. #pragma segment Vector
  87. inline int Vector::Length() const
  88. {
  89.     return (fLength);
  90. }
  91.  
  92.  
  93. //µ   Vector::_Item
  94. #pragma segment Vector
  95. inline void *Vector::_At(int i) const
  96. {
  97.     return ((*(void ***)DataArea::fHandle)[i]);
  98. }
  99.  
  100.  
  101. //µ   Vector::Item
  102. #pragma segment Vector
  103. inline void *Vector::At(int i) const
  104. {
  105.     return ((i >= 0 && i < fLength) ? _At(i) : 0);
  106. }
  107.  
  108.  
  109. //µ   Vector::_SetItem
  110. #pragma segment Vector
  111. inline void Vector::_AtPut(int i, const void *anItem)
  112. {
  113.     (*(void ***)DataArea::fHandle)[i] = anItem;
  114. }
  115.  
  116.  
  117.  
  118.  
  119. #endif
  120.  
  121.  
  122.